#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Time    : 2024/6/13 16:31
# @Author  : YangYong
# @File    : namespace_process_options_view.py
# @describe: 获取当前命名空间的流程
"""
from flask import request
from flask_restx import fields

from apis.options import option_ns
from apis.options.schema.option_schema import NameSpaceOptionSchema
from apis.options.views import option_model
from apis.orderlines.models import ProcessInstance
from public.base_model import db
from public.base_response import generate_response, MarshalModel
from public.api_utils.api_decorator import handle_api_error
from public.base_view import AbstractView

namespace_process_opt = {'namespace': fields.String()}
namespace_process_opt_marshal = MarshalModel('namespace_process_opt', option_ns)


class NamespaceProcessOptionsView(AbstractView):
    url = '/namespace_process_opt'

    def __init__(self, *args, **kwargs):
        super(NamespaceProcessOptionsView, self).__init__(*args, **kwargs)
        self.form_data: dict = request.args or {}
        self.namespace = self.form_data.get('namespace') or 'default'

    @handle_api_error()
    @option_ns.doc(description='获取当前命名空间流程')
    @option_ns.expect(namespace_process_opt_marshal.detail_request(namespace_process_opt))
    @option_ns.marshal_with(namespace_process_opt_marshal.list_response(option_model, is_db=False))
    def get(self):
        """获取当前命名空间流程"""
        objs = db.session.query(ProcessInstance).filter(
            ProcessInstance.active == 1,
            ProcessInstance.namespace == self.namespace
        ).all()
        group_owner = NameSpaceOptionSchema().dump(objs, many=True)
        return generate_response(group_owner)
